<template>
  <div
    style="
      height: 50px;
      line-height: 50px;
      border-bottom: 1px solid #ccc;
      display: flex;
    "
  >
    <div
      style="
        width: 200px;
        padding-left: 30px;
        font-weight: bold;
        color: dodgerblue;
      "
    >
      后台管理
    </div>
    <div style="flex: 1"></div>
    <div style="width: 100px; display: flex">
      <div class="avatar-container" @click="triggerUpload">
        <input type="file" ref="fileInput" @change="handleFileChange" hidden />
        <div v-if="!preview" class="upload-prompt">上传头像</div>
        <img
          v-else
          :src="preview"
          alt="Avatar Preview"
          class="avatar-preview"
        />
      </div>
      <el-dropdown>
        <span
          class="el-dropdown-link"
          style="margin-top: 10px; padding-left: 5px; outline: none"
        >
          我
          <el-icon class="el-icon--right" style="height: 25px"
            ><arrow-down
          /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人信息</el-dropdown-item>
            <el-dropdown-item>退出系统</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { ArrowDown } from "@element-plus/icons-vue";

export default {
  name: "HeaderUI",
  components: {
    ArrowDown,
  },
  data() {
    return {
      preview: null, // 用于显示预览图像
    };
  },
  methods: {
    triggerUpload() {
      this.$refs.fileInput.click(); // 触发input:file的点击事件
    },
    handleFileChange(event) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.preview = e.target.result; // 显示预览图像
        };
        reader.readAsDataURL(file); // 读取文件并将其转换为Base64编码字符串
      }
    },
  },
};
</script>

<style scoped>
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}

.avatar-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background-color: #f0f0f0; /* 轻灰色背景 */
  cursor: pointer;
  overflow: hidden; /* 确保内容不会溢出容器边界 */
  position: relative;
  text-align: center;
  line-height: 50px; /* 垂直居中显示文字 */
  color: #888; /* 文字颜色 */
}

.avatar-preview {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 保证图片覆盖整个容器但可能会被裁剪 */
}

.upload-prompt {
  width: 100%;
  height: 100%;
}
</style>
